<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * Controller for managing login credentials.
 * @package backend-authentication/controllers
 * @category Controller
 *
 * @author Mark Angelo Angulo
 * @copyright (c) 2015
 */
class Login extends CI_Controller {
    
    /**
     *  Constructor method.
     */
    function __construct()
    {
        parent::__construct();

        $this->load->config('cms');
        $this->load->helper('authentication');
        
        $this->load->library('ion_auth');
        $this->load->library('form_validation');
        
        // set cms configurations
        $cms                    = array();
        $cms['title']           = $this->config->item('title');
        $cms['db_prefix']       = $this->config->item('db_prefix');
        $this->cms = $cms;
        
        // retrieve more cms configuration from database
        $this->load->model('backend-config/cms_config_model');
        
        // set configuration from database
        $cms_configs = $this->cms_config_model->all()->get();
        foreach( $cms_configs as $cms_config )
        {
            $cms[ $cms_config['key'] ] = $cms_config['value'];
        }
        $this->cms = $cms;
        
        // load necessary models
        $this->load->model('backend-companies/company_model');
    }
    
    /**
     * Default method called when nothing is specified.
     * Login an existing user, using submitted credentials
     * @access public
     */
    public function index()
    {
        $this->login();
    }
    
    public function login()
    {
        $cms_config             = $this->cms;
        $default_company_id     = $this->company_model->attribute( array('name' => $cms_config['company']), 'id')->get();

        if(isset($_POST) && !empty($_POST))
        {
            $message = array();
            
            // validate form input
            $this->form_validation->set_rules('identity', 'username', 'trim|required|max_length[45]|xss_clean');
            $this->form_validation->set_rules('password', 'password', 'trim|required|max_length[45]|xss_clean');
            $this->form_validation->set_rules('company-id', 'company id', 'trim|required|numeric|max_length[10]|xss_clean');

            if ( $this->form_validation->run() == TRUE )
            {
                // Check to see if the user is logging in
                // Check for "remember me"
                $remember = (bool) $this->input->post('remember');

                if ( $this->ion_auth->login( set_value('identity'), set_value('password'), set_value('company-id'), $remember) )
                {
                    
                    header('location:' . base_url(). 'dashboard');
                    
                } else
                {
                    $message['title']       = 'Login Failed';
                    $message['class']       = 'alert-warning';
                    $message['message']     = 'Username and Password Mismatched';
                    $message['ion_message'] = $this->ion_auth->errors();

                    goto_login( $default_company_id, $message );
                }
            } else
            {
                // Username and Password Required
                $message['title']       = 'Login Failed';
                $message['class']       = 'alert-warning';
                $message['message']     = 'Username and Password Required';
                $message['ion_message'] = $this->ion_auth->errors();

                goto_login( $default_company_id, $message );
            }
        } else
        {
            // The user is not logging in so display the login page
            goto_login($default_company_id);
        }
    }
    
    public function company_login($company_url)
    {
        $company_id     = $this->company_model->attribute( array('url' => $company_url), 'id')->get();
        
        // Username and Password Required
//        $message['class']       = 'alert-warning';
//        $message['title']       = 'Login Failed';
//        $message['message']     = 'Username and Password Required';
//        $message['ion_message'] = $this->ion_auth->errors();

        goto_login( $company_id );
    }
}

/* End of file login.php */
/* Location: ./application/modules/backend-authentication/controllers/login.php */